package com.zhj.psi.core.mvc;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
import org.apache.commons.lang.time.DateUtils;

import java.io.IOException;

/**
 * 支持多个格式的字符串日期转换
 *
 */
public class CustomDateDeseralizer extends UntypedObjectDeserializer {

	private static final long serialVersionUID = 1L;

	@Override
	public Object deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException {
		
		if (jp.getCurrentTokenId() == JsonTokenId.ID_STRING) {
            try {
                return DateUtils.parseDate(jp.getText(), new String[] {
                        "yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:SS" });
            } catch (Exception e) {
                return super.deserialize(jp, ctxt);
            }
        } else {
            return super.deserialize(jp, ctxt);
        }
	}
	
	

}
